package ticTacToe;

public class Board {

	private Piece[][] ticTacToeBoard;
	private final int boardsize = 3;
	private int slotFree = 0;

	public Board() {
		System.out.println("Board.Board()");
		ticTacToeBoard = new Piece[boardsize][boardsize];
		boardInit();

	}

	public void addPiece(Piece piece) {

		ticTacToeBoard[piece.getPosition().getCoords()[0]][piece.getPosition()
				.getCoords()[1]] = piece;
		slotFree--;
	}

	/**
	 * 
	 * @return
	 */
	public int getBoardsize() {
		return boardsize;
	}

	/**
	 * 
	 * @return
	 */
	public Piece[][] getMyboard() {
		return ticTacToeBoard;
	}

	/**
	 * initilize the board to contain emptyPieces
	 */
	public void boardInit() {

		for (int row = 0; row < ticTacToeBoard.length; row++) {
			for (int column = 0; column < ticTacToeBoard[row].length; column++) {
				ticTacToeBoard[row][column] = new EmptyPiece(new Position(row,
						column));
				slotFree++;
			}
		}

	}

	public void removeAllPieces() {
		for (int row = 0; row < ticTacToeBoard.length; row++) {
			for (int column = 0; column < ticTacToeBoard[row].length; column++) {
				ticTacToeBoard[row][column] = null;
			}
		}

	}

	/**
	 * 
	 * @return
	 */
	public int getSlotFree() {
		return slotFree;
	}

	/*
	 * looks if the slot is free for insertion
	 */
	public boolean isSlotFree(Position position) {
		boolean isSlotFree = false;

		if (ticTacToeBoard[position.getCoords()[0]][position.getCoords()[1]]
				.getSymbol() == 'E') {
			isSlotFree = true;
		}

		return isSlotFree;
	}

	/**
	 * 
	 */
	public void displayBoard() {
		System.out.println("\ndisplaying the board");

		System.out.println("-------------------------");
		for (int row = 0; row < ticTacToeBoard.length; row++) {
			for (int column = 0; column < ticTacToeBoard[row].length; column++) {
				System.out.print("" + ticTacToeBoard[row][column] + " \t|");

			}

			System.out.println();
		}

		System.out.println("-------------------------");

	}

}
